package com.hitqz.robot.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hitqz.robot.api.business.entity.CvdStoveEntity;
import com.hitqz.robot.api.business.dto.CvdStoveQueryDTO;
import com.hitqz.robot.api.business.dto.CvdStoveSaveDTO;
import com.hitqz.robot.api.business.dto.CvdStoveUpdateDTO;
import com.hitqz.robot.api.business.entity.CvdStoveHistoryEntity;
import com.hitqz.robot.api.business.vo.CvdStoveVO;
import com.hitqz.robot.biz.mapper.CvdStoveMapper;
import com.hitqz.robot.biz.service.CvdStoveHistoryService;
import com.hitqz.robot.biz.service.CvdStoveService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import lombok.RequiredArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * CVD炉子管理
 *
 * @author kehanjiang
 * @date 2025-09-19 10:48:41
 */
@Service
@RequiredArgsConstructor
public class CvdStoveServiceImpl extends ServiceImpl<CvdStoveMapper, CvdStoveEntity> implements CvdStoveService {

    private final CvdStoveHistoryService cvdStoveHistoryService;


    private CvdStoveVO convertToVO(Object o) {
        if (o instanceof CvdStoveEntity) {
            CvdStoveVO vo = new CvdStoveVO();
            BeanUtil.copyProperties(o, vo);
            //设置其他参数

            return vo;
        }
        return null;
    }

    private void preCheck(CvdStoveEntity entity, Boolean update) {
        Long id = entity.getId();
        //String name = entity.getName();
        if (update) {//更新操作
            Assert.notNull(id, "id不可为空！");
        } else {
            //Assert.hasText(name, "name不可为空！");
        }
    }

    @Override
    public Page<CvdStoveVO> getCvdStovePage(Page page, CvdStoveQueryDTO dto) {
        CvdStoveEntity entity = new CvdStoveEntity();
        BeanUtil.copyProperties(dto, entity);
        //entity.setName(null);
        //final String name = dto.getName();
        LambdaQueryWrapper<CvdStoveEntity> wrapper = new LambdaQueryWrapper<>(entity);
        //wrapper.like(StringUtils.hasText(name), CvdStoveEntity::getName, name);
        wrapper.orderByAsc(CvdStoveEntity::getId);
        return (Page<CvdStoveVO>) this.page(page, wrapper)
                .convert(this::convertToVO);
    }

    @Override
    public CvdStoveVO getCvdStoveById(Long id) {
        Assert.notNull(id, "id不可为空！");
        return convertToVO(this.getById(id));
    }


    @Override
    public Boolean saveCvdStove(CvdStoveSaveDTO dto) {
        CvdStoveEntity entity = new CvdStoveEntity();
        BeanUtil.copyProperties(dto, entity);
        this.preCheck(entity, false);
        return this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateCvdStoveById(CvdStoveUpdateDTO dto) {
        CvdStoveEntity entity = this.getById(dto.getId());
        BeanUtil.copyProperties(dto, entity, CopyOptions.create().ignoreNullValue());
        this.preCheck(entity, true);
        String status = entity.getStatus();
        if (StringUtils.hasText(status)) {
            if ("1".equals(status)) {//开炉
                entity.setExecNo(UUID.fastUUID().toString().replaceAll("-", ""));
                //新增一条炉子运行记录
                CvdStoveHistoryEntity cvdStoveHistory = new CvdStoveHistoryEntity();
                BeanUtil.copyProperties(entity, cvdStoveHistory);
                cvdStoveHistory.setId(null);
                cvdStoveHistory.setStoveId(entity.getId());
                cvdStoveHistoryService.save(cvdStoveHistory);
            } else {//关炉
                LocalDateTime lastRunningTime = LocalDateTime.now();
                entity.setLastRunningTime(lastRunningTime);
                //更新炉子实际关炉时间
                String execNo = entity.getExecNo();
                cvdStoveHistoryService.lambdaUpdate()
                        .eq(CvdStoveHistoryEntity::getExecNo, execNo)
                        .set(CvdStoveHistoryEntity::getStatus, status)
                        .set(CvdStoveHistoryEntity::getLastRunningTime, lastRunningTime)
                        .set(CvdStoveHistoryEntity::getActualEndTime, lastRunningTime)
                        .set(CvdStoveHistoryEntity::getUpdateTime,LocalDateTime.now())
                        .update();
            }
        }
        return this.updateById(entity);
    }

    @Override
    public Boolean removeCvdStoveBatchByIds(List<Long> ids) {
        Assert.notEmpty(ids, "ID不可为空！");
        return this.removeBatchByIds(ids);
    }
}